package verify

import (
	"server/game/cache"
	"server/game/model"
	"server/share/config"

	"github.com/hwcer/cosgo"
	"github.com/hwcer/updater"
	"github.com/hwcer/yyds/players/verify"
)

func init() {
	cosgo.On(cosgo.EventTypLoaded, func() error {
		verify.SetMethod(config.Data.Functions.UnitLevelCountNum, UnitLevelCountNum)
		verify.SetMethod(config.Data.Functions.UnitGet, UnitCollectCountNum)
		return nil
	})
}

func UnitLevelCountNum(u *updater.Updater, value verify.Value) (r int64) {
	items := cache.GetItems(u)
	args := value.GetArgs()
	if len(args) == 0 {
		return 0
	}

	items.RangeWithIType(config.ITypeUnit, func(s string, v *model.Items) bool {
		if v.Attach.GetInt32(model.AttachUnitLv) >= args[0] {
			r += 1
		}
		return true
	})

	return
}

// UnitCollectCountNum 收集数量,包括幼崽~~~
func UnitCollectCountNum(u *updater.Updater, value verify.Value) (r int64) {

	//items := cache.GetItems(u)
	//args := value.GetArgs()
	//if len(args) == 0 {
	//	return 0
	//}
	//iid := args[0]
	//r = items.Val(iid)
	////统计幼体
	//if uc := config.Data.Unit[iid]; uc != nil && uc.BabyId != 0 {
	//	r += items.Val(uc.BabyId)
	//}

	//return items.Count(config.ITypeUnit, config.ITypeUnitBase)

	coll := u.Collection(config.ITypeCollect)

	for _, k := range value.GetArgs() {
		coll.Select(k)
	}
	if err := u.Data(); err != nil {
		u.Error = err
		return 0
	}

	for _, k := range value.GetArgs() {
		r += coll.Val(k)
	}

	return
}
